module(..., package.seeall)

--====================================================================--
-- SCENE: INVENTORY
--====================================================================--

--[[

 - Version: 1.3
 - Made by Ricardo Rauber Pereira @ 2010
 - Blog: http://rauberlabs.blogspot.com/
 - Mail: ricardorauber@gmail.com

******************
 - INFORMATION
******************

  - Sample scene.

--]]
local imgPath = "images/"
local myImages = {
	"jessica_100.jpg",
	"andy_100.jpg",
	"aya_100.jpg",
	"jessica_100.jpg",
	"jessica_100.jpg",
	"jessica_100.jpg",
	"jessica_100.jpg"
}	

new = function ( params )
	
	------------------
	-- Params
	------------------
	
	local vLabel = "Touch to go back"
	local vReload = false
	local start_grid = 1
	--
	if type( params ) == "table" then
		--
		if type( params.label ) == "string" then
			vLabel = params.label
		end
		--
		if type( params.reload ) == "boolean" then
			vReload = params.reload
		end
		--
		if type( params.start_grid ) == "integer" then
			start_grid = params.start_grid
		else
			start_grid = 1
		end
		
	end
	
	------------------
	-- Groups
	------------------
	
	local localGroup = display.newGroup()
	
	------------------
	-- Display Objects
	------------------
	
	local background = display.newImage( "background2.png" )	
	local title      = display.newText( "- INVENTORY -", 0, 0, native.systemFontBold, 16 )
	title.x = 160
	title.y = 20

	--close button
	local btBatsut = function ( event )
		if event.phase == "release" then
			if params.back_scene == 'lab' then
				director:changeScene( { start_grid = start_grid }, params.back_scene, "crossfade" )
			else
				director:changeScene( "field", "crossfade" )
			end
		end
	end

	local btBatsu = ui.newButton{
					default = imgPath .. "cross_yellow_64.png",
					over = imgPath .. "cross_green_64.png",
					onEvent = btBatsut,
					id = "btBatsu"
	}
	btBatsu.x, btBatsu.y = 295,50
	btBatsu.xScale, btBatsu.yScale = 0.6,0.6
	--eo close button
	
	--icons
	--help icon
	local btHelpt = function ( event )
		if event.phase == "release" then
			director:openPopUp( "help_popup", popClosed )
		end
	end
	local btHelp = ui.newButton{
					default = imgPath .. "icon_help_128.png",
					over = imgPath .. "cross_green_64.png",
					onEvent = btHelpt,
					id = "btHelp"
	}
	btHelp.x, btHelp.y = 260,260
	btHelp.xScale, btHelp.yScale = 0.5,0.5
	local txtHelp = display.newText( "Help", 245, 290, native.systemFontBold, 16 )

	--token icon
	local btTokent = function ( event )
		if event.phase == "release" then
			director:openPopUp( "gettoken_popup", popClosed )
		end
	end
	local btToken = ui.newButton{
					default = imgPath .. "icon_apple_128.png",
					over = imgPath .. "cross_green_64.png",
					onEvent = btTokent,
					id = "btToken"
	}
	btToken.x, btToken.y = 155,260
	btToken.xScale, btToken.yScale = 0.4,0.4
	local txtToken = display.newText( " Get\nToken", 130, 290, native.systemFontBold, 16 )

	--potion icon
	local btPotiont = function ( event )
		if event.phase == "release" then
			director:openPopUp( "getpotion_popup", popClosed )
		end
	end
	local btPotion = ui.newButton{
					default = imgPath .. "icon_apple_128.png",
					over = imgPath .. "cross_green_64.png",
					onEvent = btPotiont,
					id = "btPotion"
	}
	btPotion.x, btPotion.y = 50,260
	btPotion.xScale, btPotion.yScale = 0.4,0.4
	local txtPotion = display.newText( " Get\nPotion", 40, 290, native.systemFontBold, 16 )
	
	--setting icon
	local btSettingt = function ( event )
		if event.phase == "release" then
			director:openPopUp( "setting_popup", popClosed )
		end
	end
	local btSetting = ui.newButton{
					default = imgPath .. "icon_help_128.png",
					over = imgPath .. "cross_green_64.png",
					onEvent = btSettingt,
					id = "btSetting"
	}
	btSetting.x, btSetting.y = 260,160
	btSetting.xScale, btSetting.yScale = 0.5,0.5
	local txtSetting = display.newText( "Setting", 245, 190, native.systemFontBold, 16 )
	
	
	--popup grid
	itemWidth = 50
	itemHeight = 50
	unit_w = 50
	unit_h = 50
	gridGroup = display.newGroup()
	function makeTiles(rowcnt,colcnt,unit_w,unit_h,cellpadding,leftmargin,topmargin)
		--gridbg
		local 	gridbg = display.newRect( 0, 0, colcnt*(unit_w+cellpadding)+leftmargin, rowcnt*(unit_h+cellpadding)+topmargin )
				gridbg:setFillColor(140, 140, 140)
				gridbg:setStrokeColor(180, 180, 180)
				gridGroup:insert(gridbg)
		
		for i = 1, rowcnt, 1 do
			for j = 1, colcnt, 1 do
				x = (j-1)*(unit_w + cellpadding)+leftmargin
				y = topmargin + (i-1)*(unit_h + cellpadding)
				local unit = display.newRect( x, y, unit_w, unit_h )
				gridGroup:insert(unit)

			end
		end
	end
	local function bb(monster_id)
		local monDetailGrp = display.newGroup()
		local bdr2 = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
		bdr2:setReferencePoint(display.BottomLeftReferencePoint) 
		bdr2.strokeWidth = 1
		bdr2:setFillColor(255, 100, 100)
		bdr2:setStrokeColor(0, 222, 222)
		bdr2.alpha = 0.1
		
		local fncProtection2 = function( event )
			return true
		end	
		bdr2:addEventListener( "tap", fncProtection2 )
		localGroup:insert(bdr2)
		
		local w, h = display.contentWidth, display.contentHeight
		local background = display.newImage( "background3.png" )
		background.xScale = 1
		background.yScale = 1
		background.alpha = 1
		--close button
		local closeBtn = display.newImage("images/cross_32.png") 
		function closeBtn:tap(e)  
			--bdr.isVisible = false;
			print("pangkah popup monster!!!")
			if monDetailGrp.numChildren > 0 then
					for i=monDetailGrp.numChildren,1,-1 do
					   monDetailGrp[i]:removeSelf()
					   monDetailGrp[i] = nil
					end
			end 
			localGroup:remove(monDetailGrp)
			monDetailGrp = nil
		end  
		closeBtn:addEventListener( "tap", closeBtn )
		--popup title
		local popupTitle = display.newText( "MONSTER : "..monster_id, 120, 5, native.systemFontBold, 20 )
		--add monster image
		local monImg = display.newImage("images/jessica_600x900.jpg")
		monImg.x = 160
		monImg.y = 255
		monImg.xScale = 0.5
		monImg.yScale = 0.5

		--add to grp
		monDetailGrp:insert(bdr2)
		monDetailGrp:insert(background)
		monDetailGrp:insert(monImg)
		monDetailGrp:insert(closeBtn)
		monDetailGrp:insert(popupTitle)
		--add to main grp
		
		-- for row in db:nrows("SELECT * FROM monster") do
		  -- local text = row.word_en.." "..row.word_my
		  -- local t = display.newText(text, 20, 30 * row._id, null, 16)
		  -- t:setTextColor(255,0,255)
		  -- monDetailGrp:insert(t)
		-- end
		localGroup:insert(monDetailGrp)

		--eo close button
	end
	function popMonster()
		print("popupmonster")
		bb("Jasika")
	end
	function putGrid(row,column,img,grid,cellpadding,leftmargin,topmargin)
		local  item = display.newImageRect( img, itemWidth, itemHeight )
		item.x = (column-1)*(unit_w + cellpadding) + leftmargin+25
		item.y = topmargin + (row-1)*(unit_h + cellpadding) + 25
		item:addEventListener("tap", popMonster)
		grid:insert(item)
	end
	local function aa()
		local bdr = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
		bdr:setReferencePoint(display.BottomLeftReferencePoint) 
		bdr.strokeWidth = 1
		bdr:setFillColor(100, 100, 100)
		bdr:setStrokeColor(0, 222, 222)
		bdr.alpha = 0.5
		
		local fncProtection = function( event )
			return true
		end	
		bdr:addEventListener( "touch", fncProtection )

		local closeBtn = display.newImage("images/cross_32.png") 
		function closeBtn:tap(e)  
			--bdr.isVisible = false;
			print("pangkah!!!")
			bdr:removeSelf()
			fncProtection = nil
			closeBtn:removeSelf()
			--gridGroup:removeChildren(); 
			if gridGroup.numChildren > 0 then
					for i=gridGroup.numChildren,1,-1 do
					   gridGroup[i]:removeSelf()
					   gridGroup[i] = nil
					end
			end 
--			
		end  
		closeBtn:addEventListener( "tap", closeBtn )
		
		makeTiles(4,5,50,50,10,10,5)
		localGroup:insert(bdr)
		gridGroup:insert(closeBtn)
		localGroup:insert(gridGroup)
		gridGroup.x = 5
		gridGroup.y = 60
		closeBtn.x = 300
		closeBtn.y = -10
		--for i=1,#myImages,1 do
		for i=1,6,1 do
			maxrow=5
			maxcol=5
			r = math.ceil(i/maxrow) --what row?
			c = i - (r-1)*maxcol    --what column?
			putGrid(r,c,myImages[i],gridGroup,10,10,5)
			print("row=",r ,"col=", c)
		end
	end
	
	--gallery icon
	local btGalleryt = function ( event )
		if event.phase == "release" then
			--director:openPopUp( "gallery_popup", popClosed )
			aa()
		end
	end
	local btGallery = ui.newButton{
					default = imgPath .. "icon_apple_128.png",
					over = imgPath .. "cross_green_64.png",
					onEvent = btGalleryt,
					id = "btGallery"
	}
	btGallery.x, btGallery.y = 155,160
	btGallery.xScale, btGallery.yScale = 0.4,0.4
	local txtGallery = display.newText( " Gallery", 130, 190, native.systemFontBold, 16 )

	--mystats icon
	local btMystatt = function ( event )
		if event.phase == "release" then
			director:openPopUp( "mystat_popup", popClosed )
		end
	end
	local btMystat = ui.newButton{
					default = imgPath .. "icon_apple_128.png",
					over = imgPath .. "cross_green_64.png",
					onEvent = btMystatt,
					id = "btMystat"
	}
	btMystat.x, btMystat.y = 50,160
	btMystat.xScale, btMystat.yScale = 0.4,0.4
	local txtMystat = display.newText( "My Stats", 20, 190, native.systemFontBold, 16 )
	--eo icons
	
	------------------
	-- Listeners
	------------------
	
	local touched = function ( event )
		if event.phase == "ended" then
			if vReload then
				director:changeScene( { label="Scene Reloaded" }, "inventory","moveFromRight" )
			else
				director:changeScene( "field", "crossfade" )
			end
		end
	end
	
	--====================================================================--
	-- INITIALIZE
	--====================================================================--
	
	local initVars = function ()
		
		------------------
		-- Inserts
		------------------
		
		localGroup:insert( background )
		localGroup:insert( title )
		
		------------------
		-- Positions
		------------------
				
		------------------
		-- Colors
		------------------
		
		title:setTextColor( 255,255,255 )
		
		------------------
		-- Listeners
		------------------
		
		--background:addEventListener( "touch" , touched )
		
	end
	
	------------------
	-- Initiate variables
	------------------
	
	initVars()
	
	------------------
	-- MUST return a display.newGroup()
	------------------
	
	return localGroup
	
end
